#include <Arduino.h>

// 2022.6.14——ESP01S 的blinker 数字显示num(无会员)调试完成
/* ////////////////////////////////////////////////
    实验板：ESP01S
    版本：V0.2
    更改内容:
        V0.1：blinker按键开关调试完成
        V0.2：blinker数字显示num(无会员)调试完成
*/
////////////////////////////////////////////////

#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#define serial_data_num 5
#define ON 1
#define OFF 2

#include <Blinker.h>

char auth[] = "e8aa3e63d644";
char ssid[] = "123456";
char pswd[] = "776654321";

// 新建按键对象和对应变量
char b_on;
BlinkerButton Button_on("b_on");
char b_lsens;
BlinkerButton Button_lsens("b_lsens");
char b_dth;
BlinkerButton Button_dth("b_dth");
char b_711;
BlinkerButton Button_711("b_711");

//新建数字显示对象和对应变量
int n_lsens=0;
BlinkerNumber Number_lsens("n_lsens");
int n_tem=0;
BlinkerNumber Number_tem("n_tem");
int n_hum=0;
BlinkerNumber Number_hum("n_hum");
int n_711=0;
BlinkerNumber Number_711("n_711");
int n_remote=0;
BlinkerNumber Number_remote("n_remote");



// 舵机按键开关回调函数
void button_on_callback(const String &state)
{
    // BLINKER_LOG("get button state: ", state); //串口打印get button state:
    if (state == "on")
    {
        digitalWrite(LED_BUILTIN, LOW);
        b_on=ON;
        Button_on.color("#1E90FF");                 //蓝色
        Button_on.icon("fas fa-angle-double-down"); //图标变化
        Button_on.text("衣架已升起");                 //文字显示
        Button_on.print("on");                      // 反馈开关状态
    }
    else if (state == "off")
    {
        digitalWrite(LED_BUILTIN, HIGH);
        b_on=OFF;
        Button_on.color("#1E90FF");
        Button_on.icon("fas fa-angle-double-up");
        Button_on.text("衣架已降下");
        Button_on.print("off"); // 反馈开关状态
    }
}
// 光敏按键开关回调函数
void button_lsens_callback(const String &state)
{
    // BLINKER_LOG("get button state: ", state); //串口打印get button state:
    if (state == "on")
    {
        digitalWrite(LED_BUILTIN, LOW);
        b_lsens=ON;
        Button_lsens.color("#1E90FF");                 //蓝色
        Button_lsens.icon("fas fa-lightbulb"); //图标变化
        Button_lsens.text("光敏模块开启");                 //文字显示
        Button_lsens.print("on");                      // 反馈开关状态
    }
    else if (state == "off")
    {
        digitalWrite(LED_BUILTIN, HIGH);
        b_lsens=OFF;
        Button_lsens.color("#1E90FF");
        Button_lsens.icon("far fa-lightbulb-slash");
        Button_lsens.text("光敏模块关闭");
        Button_lsens.print("off"); // 反馈开关状态
    }
}
//温湿度按键开关回调函数
void button_dth_callback(const String &state)
{
    // BLINKER_LOG("get button state: ", state); //串口打印get button state:
    if (state == "on")
    {
        digitalWrite(LED_BUILTIN, LOW);
        b_dth=ON;
        Button_dth.color("#1E90FF");                 //蓝色
        Button_dth.icon("fas fa-tint"); //图标变化
        Button_dth.text("温湿度检测开启");                 //文字显示
        Button_dth.print("on");                      // 反馈开关状态
    }
    else if (state == "off")
    {
        digitalWrite(LED_BUILTIN, HIGH);
        b_dth=OFF;
        Button_dth.color("#1E90FF");
        Button_dth.icon("fas fa-tint-slash");
        Button_dth.text("温湿度检测关闭");
        Button_dth.print("off"); // 反馈开关状态
    }
}
// 称重按键开关回调函数
void button_711_callback(const String &state)
{
    // BLINKER_LOG("get button state: ", state); //串口打印get button state:
    if (state == "on")
    {
        digitalWrite(LED_BUILTIN, LOW);
        b_711=ON;
        Button_711.color("#1E90FF");                 //蓝色
        Button_711.icon("fas fa-hand-holding-water"); //图标变化
        Button_711.text("干湿检测开启");                 //文字显示
        Button_711.print("on");                      // 反馈开关状态
    }
    else if (state == "off")
    {
        digitalWrite(LED_BUILTIN, HIGH);
        b_711=OFF;
        Button_711.color("#1E90FF");
        Button_711.icon("fas fa-hand-holding-water");
        Button_711.text("干湿检测关闭");
        Button_711.print("off"); // 反馈开关状态
    }
}



// 如果未绑定的组件被触发，则会执行其中内容
void dataRead(const String &data)
{
    // BLINKER_LOG("Blinker readString: ", data);
    // counter++;
    // Number1.print(counter);
}
//心跳包，用于发送数据同步（无会员使用心跳包同步数据，每1分钟同步一次数据）
void heartbeat()
{
    Number_lsens.print(n_lsens);
    Number_tem.print(n_tem);
    Number_hum.print(n_hum);
    Number_711.print(n_711);
    Number_remote.print(n_remote);
}

void setup()
{
    //-1.初始化串口
    Serial.begin(115200);
#if defined(BLINKER_PRINT)
    BLINKER_DEBUG.stream(BLINKER_PRINT);//用于指定调试信息输出的串口，设备开发时调试使用
#endif
    //-2.初始化有LED的IO
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    //-3.初始化blinker
    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    //按键开关绑定
    Button_on.attach(button_on_callback);
    Button_lsens.attach(button_lsens_callback);
    Button_dth.attach(button_dth_callback);
    Button_711.attach(button_711_callback);

    Blinker.attachHeartbeat(heartbeat);

}

void loop()
{
    Blinker.run();
    n_lsens++;
    n_tem++;
    n_hum++;
    n_711++;
    n_remote++;
    Blinker.delay(1000);
}
